﻿
open System
open System.IO
open Commands
open Output
open Execution

#if DEBUG
let Defaults = 
   [|
      {StringCommand.Name = "-behaviors:"; Value = @"..\..\..\Forethought.Core.Behaviors\Behaviors.txt"}
      {StringCommand.Name = "-language:"; Value = @"..\..\..\Forethought.Core.Behaviors\bin\Debug\Forethought.Core.Behaviors.dll"}
      {StringCommand.Name = "-output:"; Value = "console"}
      {StringCommand.Name = "-format:"; Value = "text"}      
      {StringCommand.Name = "-wait"; Value = "true"}
   |]
#else
let Defaults = 
   [|
      {StringCommand.Name = "-output:"; Value = "console"}
      {StringCommand.Name = "-format:"; Value = "text"}      
      {StringCommand.Name = "-wait"; Value = "true"}
   |]
#endif

let PrintCommands(commands : StringCommand[]) = 
   commands |> Array.iter(fun c -> (printfn @"   %s %s" c.Name c.Value))

[<EntryPoint>]
let main argv = 
   printfn "Forethought Runner"
   printfn "=================="
   printfn ""

   let updates = ReadCommands(argv)
   let commands = UpdateCommands(Defaults, updates)

   printfn "Configuration:"
   PrintCommands(commands)
   printfn ""

   let behaviors = new FileInfo(FindCommand("-behaviors:", commands).Value)
   let language = new FileInfo(FindCommand("-language:", commands).Value)
   let dependencies = 
      TryFindCommand("-dependencies:", commands, "").Value.Split(';') 
      |> Array.filter(fun f -> String.IsNullOrWhiteSpace(f) <> true)
      |> Array.map(fun f -> new FileInfo(f))

   let output = CreateOutput(FindCommand("-output:", commands).Value)
   //let format = FindCommand("-format:", commands)
   let wait = TryFindCommand("-wait", commands, "true").Value

   let context = CreateContext(language, dependencies)

   ExecuteFile(behaviors, context, output)
   
   if (wait = "true") then
      Console.ReadLine() |> ignore

   0 // return an integer exit code
